package edu.gtcfla.km.appmodules.web;

import java.util.LinkedList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vsg.biz.BusinessOperationException;
import org.vsg.common.i18n.I18nMessageService;
import org.vsg.common.web.json.SubmitResult;
import org.vsg.security.annotation.RegisterRequestPermissionStrategy;

import com.fasterxml.jackson.databind.ObjectMapper;

import edu.gtcfla.km.appmodules.service.AttachmentService;
import edu.gtcfla.km.appmodules.service.KnowledgeService;
import edu.gtcfla.km.appmodules.service.UploadFileManager;
import edu.gtcfla.km.appmodules.service.UploadFileService;

@Controller
@Lazy
@RequestMapping(edu.gtcfla.km.core.ApplicationConstant.DATA_RERESOURCE)
public class AttachmentController {

	
	private final String modulePath = "/knowledge";		
	
	@Autowired(required = false)
	private UploadFileService uploadFileService;

	@Autowired(required = false)
	private UploadFileManager uploadFileManager;
	
	@Autowired(required=false)
	private KnowledgeService knowledgeService;	

	@Autowired(required = false)
	private I18nMessageService i18nMessageService;

	@Autowired(required = false)	
	private AttachmentService attachmentService;
		

	private ObjectMapper mapper = new ObjectMapper();

	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath+"/content/submit_deletefiles.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath+"/content/submit_deletefiles.json")
	public @ResponseBody SubmitResult accessDeleteFiles(@RequestBody  String[] receiveValues) throws Exception {
		
		// --- map to json object data ---
		SubmitResult result = new SubmitResult();
		
		try {
			
			attachmentService.deleteFiles( receiveValues );
			
			result.setSuccess(true);
			//String msg = i18nMessageService.getMessage("module-client.company.add.success", null);
			result.setMsg("删除附件成功");
			
		} catch (BusinessOperationException e) {

			e.printStackTrace();
			result.setSuccess(false);
			String msg = i18nMessageService.getMessage("module-client.company.add.error", null);
			result.setMsg(msg);

		
		}

		return result;
	}	


	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath +  "/attachment/submit_bindcolligation.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath +  "/attachment/submit_bindcolligation.json")
	public @ResponseBody SubmitResult bindAttachmentToColligation( String colligationId , String selectedAttachment) throws Exception {
		
		List<String> receiveValues =new LinkedList<String>();
		if (selectedAttachment != null) {
			
			if ( selectedAttachment.indexOf(",") > -1) {
				String[] attchmenColls = selectedAttachment.split(",");
				for (int index = 0 ; index < attchmenColls.length ; index++ ) {
					receiveValues.add( attchmenColls[index] );
				}
			} else {
				receiveValues.add( selectedAttachment );
			}
			
		}
		
		
		// --- map to json object data ---
		SubmitResult result = new SubmitResult();

		// --- remove user id ---
		try {

			if (receiveValues != null && receiveValues.size() > 0) {

				knowledgeService.bindAttachments(receiveValues, colligationId);
			}

			result.setSuccess(true);
/*
			String msg = i18nMessageService.getMessage(
					"module-admin.user.delete.success", null);
					*/
			result.setMsg("绑定知识点");

		} catch (BusinessOperationException se) {
			// TODO Auto-generated catch block
			se.printStackTrace();
			result.setSuccess(false);
			String msg = i18nMessageService.getMessage(
					"module-admin.user.delete.success", null);
			result.setMsg(msg);
		}



		return result;
	}
	
	
}
